package org.gdufe.logs.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.gdufe.logs.service.LogService;
import org.gdufe.pojos.dto.InsertLogDto;
import org.gdufe.pojos.dto.InsertUserDto;
import org.gdufe.pojos.pojo.Log;
import org.gdufe.pojos.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ConsumerService {
    @Autowired
    private LogService logService;

    @Component//引用配置文件中的数据
    //@RocketMQMessageListener声明一个消息消费者
    //topic需要和生产者的topic一致；consumerGroup属性必须指定，内容可随意；messageModel默认为CLUSTERING 集群消费，BROADCASTING 为广播消费
    @RocketMQMessageListener(nameServer = "${rocketmq.name-server}", topic = "createBorrowLogMsg", consumerGroup = "${rocketmq.consumer.group0}", messageModel = MessageModel.CLUSTERING)
    class ConsumerInGroup0 implements RocketMQListener<Object> {
        @Override
        public void onMessage(Object o) {
            System.out.println("收到的json:  "+o);
            Log log = JSON.parseObject((String) o, Log.class);
            System.out.println("接收到borrowlog:"+log);
            R r = logService.borrowLog(log);
            System.out.println("【borrowlog消费结果】："+r);
        }
    }

    @Component//引用配置文件中的数据
    //@RocketMQMessageListener声明一个消息消费者
    //topic需要和生产者的topic一致；consumerGroup属性必须指定，内容可随意；messageModel默认为CLUSTERING 集群消费，BROADCASTING 为广播消费
    @RocketMQMessageListener(nameServer = "${rocketmq.name-server}", topic = "createReturnLogMsg", consumerGroup = "${rocketmq.consumer.group1}", messageModel = MessageModel.CLUSTERING)
    class ConsumerInGroup1 implements RocketMQListener<Object> {
        @Override
        public void onMessage(Object o) {
            System.out.println("收到的json:  "+o);
            Log log= JSON.parseObject((String)o,Log.class);
            System.out.println("接收到returnlog:"+log);
            R r = logService.returnLog(log);
            System.out.println("【returnlog消费结果】："+r);
        }
    }

}
